import grails.test.*

class UserAccountControllerUnitTests extends ControllerUnitTestCase {

  def controller

  UserAccountController userAccountController

  UserAccountControllerUnitTests() {
      super(UserAccountController)
  }

  
  protected void setUp() {
    super.setUp()
    controller = new UserAccountController()

    // Initialise the user service.
    def notifierService = new NotifierService()

    controller.mailService = notifierService

  }

  protected void tearDown() {
    super.tearDown()
  }

  void testList() {
    List userAccountInstanceList = []
    20.times {i -> userAccountInstanceList << new UserAccount(firstName: "D" + i, lastName: "M" + i, email: "d" + i + "@g.com", password: "pass" + i) }
    mockDomain(UserAccount, userAccountInstanceList)
    Map model = controller.list()
    assertNotNull('list should be present in model', model.userAccountInstanceList)
    assertEquals('list size should match default', 10, model.userAccountInstanceList.size())
  }

  void testShow() {
    UserAccount expectedUserAccount = new UserAccount(firstName: "D", lastName: "M", email: "d@g.com", password: "pass")
    mockDomain(UserAccount, [expectedUserAccount])
    controller.params.id = 1
    assertNotNull(controller)
    assertNotNull(controller.params)
    assertNotNull(controller.params.id)
    Map model = controller.show()
    assertEquals(expectedUserAccount, model.userAccountInstance)
  }

  void testEdit() {
    UserAccount expectedUserAccount = new UserAccount(firstName: "D", lastName: "M", email: "d@g.com", password: "pass")
    mockDomain(UserAccount, [expectedUserAccount])
    controller.params.id = 1
    Map model = controller.edit()
    assertEquals('model.userAccountInstance', expectedUserAccount, model.userAccountInstance)
  }

  void testEdit_userAccountInstanceNotFound() {
    mockDomain(UserAccount)
    controller.params.id = 999 // no object with id 999 exists
    controller.edit()
    assertEquals('redirect action', controller.list, redirectArgs.action)
    assertEquals('flash message', "UserAccount not found with id ${controller.params.id}", mockFlash.message)
  }

  //update

  //create
  void testCreate() {
    mockDomain(UserAccount)
    Map model = controller.create()
    assertNotNull('userAccountInstance', model.userAccountInstance)
  }

  //save
  void testSave() {
    UserAccount expectedUserAccount = new UserAccount(firstName: "D", lastName: "M", email: "d@g.com", password: "pass")
    mockDomain(UserAccount, [expectedUserAccount])
    controller.params.email = expectedUserAccount.email
    UserAccount.metaClass.save = {-> return true}

    //TODO folowing emits java.lang.NullPointerException: Cannot invoke method sendMail() on null object
    controller.save()

    sessionFactory.currentSession.flush()
    sessionFactory.currentSession.clear()


    //assertEquals('redirect action', controller.show, redirectArgs.action)
    //assertEquals('redirect id', controller.params.id, redirectArgs.id)
    //assertEquals('flash message', "UserAccount ${controller.userAccountInstance.email} created", mockFlash.message)
  }


}